package cycles.util.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfReader {
	boolean local;
	String root;

	public String getRootPath() {
		return this.root;
	}

	public ConfReader(String rootpath, boolean fromdisk) {
		this.local = fromdisk;
		this.root = rootpath;
		if (!root.endsWith("/")) {
			root = root + "/";
		}

	}

	/**
	 * 加载一个xml文件的root
	 * 
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public Element getXmlRoot(String filename) throws Exception {
		Element rootele = null;

		if (this.local) {
			SAXReader reader = new SAXReader();
			Document document = reader.read(root + filename);
			rootele = document.getRootElement();
		} else {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(root + filename));
			rootele = document.getRootElement();
		}
		return rootele;

	}

	/**
	 * 加载一个properties的文件
	 * 
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public Properties getProperties(String filename) throws Exception {
		Properties prop = null;
		if (local) {
			URL url = new URL(root + filename);
			prop = new Properties();
			InputStream in = url.openStream();
			prop.load(in);
			in.close();

		} else {
			InputStream in = new BufferedInputStream(new FileInputStream(root
					+ filename));
			prop = new Properties();
			prop.load(in);

		}

		return prop;

	}

	/**
	 * 将远程文件写到当前工作目录,二进制及文本均可
	 * 
	 * @param filename
	 * @return
	 */
	public void conf2local(String destpath, String filename) throws Exception {

		if (!destpath.endsWith("/")) {
			destpath += "/";
		}
		FileOutputStream out = new FileOutputStream(destpath + filename);
		InputStream in = null;
		DataInputStream ins = null;
		if (local) {
			in = new FileInputStream(root + filename);
			ins = new DataInputStream(in);
		} else {
			URL url = new URL(root + filename);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			in = connection.getInputStream();
			ins = new DataInputStream(in);
		}
		byte[] buff = new byte[1024];
		int i = 0;
		while ((i = ins.read(buff)) >= 0) {
			out.write(buff, 0, i);
		}
		out.close();
		in.close();
	}

	/**
	 * 自定义格式的配置文件可以将整个文件作为字符串读出来
	 * 
	 * @param filename
	 * @return
	 * @throws Exception
	 */
	public String getString(String filename) throws Exception {
		String ret = null;
		InputStream in = null;
		if (local) {
			in = new FileInputStream(root + filename);
		} else {
			URL url = new URL(root + filename);
			in = url.openStream();
		}
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		ret = "";
		String line = null;
		while ((line = reader.readLine()) != null) {
			ret += line;
		}
		in.close();
		return ret;

	}

}
